

from xpy3lib.XRetryableQuery import XRetryableQuery
from xpy3lib.XRetryableSave import XRetryableSave
from sicost.AbstractDPJob import AbstractDPJob
import sys, datetime, json, math, time
import pandas as pd
import numpy as np

from numpy import array


class AlignJob(AbstractDPJob):


    def __init__(self,
                 p_dff=None,
                 p_Union_start_time=None,
                 p_Union_end_time=None,
                 p_downTime=None):


        super(AlignJob, self).__init__()

        self.dff = p_dff
        self.Union_start_time = p_Union_start_time
        self.Union_end_time = p_Union_end_time
        self.downTime = p_downTime


        pass


    def execute(self):
        return self.do_execute()


    def do_execute(self):

        super(AlignJob, self).do_execute()

        """
        对齐数据Job
        
        """
        new_freq = int(self.downTime * 60)
        # new_freq = 310
        new_freq = str(new_freq)

        ret2 = time.localtime(self.Union_start_time / 1000)
        Union_start_time = time.strftime("%Y-%m-%d %H:%M:%S", ret2)
        ret3 = time.localtime(self.Union_end_time / 1000)
        Union_end_time = time.strftime("%Y-%m-%d %H:%M:%S", ret3)

        helper = pd.DataFrame(
            {'datetime': pd.date_range(start=Union_start_time, end=Union_end_time,
                                       freq=new_freq + 's'), 'remark': 1})
        # helper = pd.DataFrame(helper).set_index('datetime')#将时间列变为索引

        d = pd.merge(self.dff, helper, on='datetime', how='outer').sort_values('datetime')
        d['datetimeindex'] = d['datetime']
        d.set_index('datetimeindex', inplace=True)
        # d.reindex('datetimeindex', axis='columns')
        d['value'] = d['value'].interpolate(method='nearest',fill_value='extrapolate')
        dd = d[d['remark'] == 1]
        dd.drop(['remark'], axis=1, inplace=True)
        dd = dd.reset_index(drop=True)
        dd['datetime_new'] = dd['datetime'].astype("string")

        # dd['timestamp'].fillna(value=0.0, inplace=True)

        def __cal_datetime2timestamp(x):

            timeArray = time.strptime(x.datetime_new, "%Y-%m-%d %H:%M:%S")
            ret = int(time.mktime(timeArray))
            rst = ret * 1000
            return rst

        dd['timestamp_new'] = dd.apply(lambda x: __cal_datetime2timestamp(x), axis=1)

        dd.drop(['datetime_new'], axis=1, inplace=True)
        dd.drop(['timestamp'], axis=1, inplace=True)
        dd.rename(columns={'timestamp_new': 'timestamp'}, inplace=True)




        # elapsed_min = self.dff['timestamp'].min() - self.min_time
        # print(elapsed_min)
        #
        # elapsed_max = self.max_time - self.dff['timestamp'].max()
        # print(elapsed_max)
        #
        # elapsed_dff = self.dff['timestamp'].max() - self.dff['timestamp'].min()
        # print(elapsed_dff)
        #
        # num_min = math.ceil(elapsed_min/elapsed_dff)
        # num_max = math.ceil(elapsed_max / elapsed_dff)
        #
        # dfff = self.dff
        # if num_min >= 1:
        #     if num_min == 1:
        #         cut_max_time_top = dfff['timestamp'].min() + elapsed_min
        #         dff0 = dfff[dfff['timestamp'] < cut_max_time_top]
        #         dff0.rename(columns={'timestamp': 'timestamp_old'}, inplace=True)
        #         dff0['timestamp'] = dff0['timestamp_old'] - elapsed_min
        #         dff0.drop(['timestamp_old'], axis=1, inplace=True)
        #         dfff = pd.concat([dff0, dfff], ignore_index=True)
        #         dfff = dfff.sort_values('timestamp')
        #         dfff = dfff.reset_index(drop=True)
        #     else:
        #         elapsed_min_part = elapsed_min / num_min
        #         cut_max_time_top = dfff['timestamp'].min() + elapsed_min_part
        #         dff0 = dfff[dfff['timestamp'] < cut_max_time_top]
        #         dff01 = dfff
        #         for i in range(int(num_min)):
        #             dff0.rename(columns={'timestamp': 'timestamp_old'}, inplace=True)
        #             dff0['timestamp'] = dff0['timestamp_old'] - elapsed_min_part
        #             dff0.drop(['timestamp_old'], axis=1, inplace=True)
        #             dfff = pd.concat([dff0, dff01], ignore_index=True)
        #             dfff = dfff.sort_values('timestamp')
        #             dfff = dfff.reset_index(drop=True)
        # if num_max >= 1:
        #     if num_max == 1:
        #         cut_max_time_tail = self.dff['timestamp'].max() - elapsed_max
        #         dff0 = self.dff[self.dff['timestamp'] > cut_max_time_tail]
        #         dff0.rename(columns={'timestamp': 'timestamp_old'}, inplace=True)
        #         dff0['timestamp'] = dff0['timestamp_old'] + elapsed_max
        #         dff0.drop(['timestamp_old'], axis=1, inplace=True)
        #         dfff = pd.concat([dff0, dfff], ignore_index=True)
        #         dfff = dfff.sort_values('timestamp')
        #         dfff = dfff.reset_index(drop=True)
        #     else:
        #         elapsed_max_part = elapsed_max / num_max
        #         cut_max_time_tail = self.dff['timestamp'].max() - elapsed_max_part
        #         dff0 = self.dff[self.dff['timestamp'] > cut_max_time_tail]
        #         dff01 = dfff
        #         for i in range(int(num_max)):
        #             dff0.rename(columns={'timestamp': 'timestamp_old'}, inplace=True)
        #             dff0['timestamp'] = dff0['timestamp_old'] + elapsed_max_part
        #             dff0.drop(['timestamp_old'], axis=1, inplace=True)
        #             dfff = pd.concat([dff0, dff01], ignore_index=True)
        #             dfff = dfff.sort_values('timestamp')
        #             dfff = dfff.reset_index(drop=True)
        #
        #
        #
        # ddd = dfff
        #
        # def __cal_timestamp2datetime(x):
        #
        #     ret2 = time.localtime(x.timestamp / 1000)
        #     rst = time.strftime("%Y-%m-%d %H:%M:%S", ret2)
        #
        #     return rst
        #
        # ddd['datetime_new'] = ddd.apply(lambda x: __cal_timestamp2datetime(x), axis=1)
        # ddd.drop(['datetime'], axis=1, inplace=True)
        # ddd.rename(columns={'datetime_new': 'datetime'}, inplace=True)
        print('finish')
        return dd